package vng.wcc.sonph.LauncherGenerator;

import android.content.Context;
import android.widget.LinearLayout;

public class LauncherViewWithTabs extends LinearLayout {

	private TopTabView topTabView;
	private LauncherView launcherView;
	private TabRowView tabRowView;

	/**
	 * LauncherViewWithTabs contains 
	 * - A top tab - contains some information - that is reserved space for the developer
	 * - A launcher view ( where items displayed) 
	 * - A bottom tab - which takes responsibility for page control 
	 * @param context
	 * @param data contains information about the items to be displayed (icon,title and intent to launch)
	 * @param tabData contains information about the items to be displayed on the tab (icon,highlight icon)
	 * @param formatter provides layout information 
	 * @param numColsPerPage number of columns per row on each page
	 * @throws Exception an exception will be thrown if the number of pages doesn't equal to number if tabs
	 */
	public LauncherViewWithTabs(Context context,CellInfo[][] data,TabCellInfo[] tabData,Formatter formatter,int numColsPerPage) throws Exception {
		super(context);
		
		//An exception will be thrown if the number of pages doesn't equal to number if tabs
		if ( data.length != tabData.length) 
			throw new Exception("Number of tabs must equal to number of screens !");		
		
		initView(context, data, tabData, formatter, numColsPerPage);

	}

	private void initView(Context context, CellInfo[][] data,
			TabCellInfo[] tabData, Formatter formatter, int numColsPerPage) {
		setViewLayout(context, data,formatter, numColsPerPage); 
		addChildren(context, data,tabData, formatter,numColsPerPage);
	}

	/**
	 * Add the children views to the view
	 * @param context
	 * @param tabData contains information about the items to be displayed on the tab (icon,highlight icon)
	 * @param formatter provides layout information
	 */
	private void addChildren(Context context, CellInfo[][] data,TabCellInfo[] tabData,
			Formatter formatter,int numColsPerPage) {
		//tabformatter provides the layout information for the tab
		
		TabFormatter tabformatter = new TabFormatter(formatter, tabData.length);
		
		this.topTabView = new TopTabView(this.getContext());
		this.tabRowView = new TabRowView(context,tabData, formatter);
		this.launcherView = new LauncherView(context, data,formatter,tabRowView, numColsPerPage);
		this.tabRowView.attachToLauncher(launcherView);
		
		this.addView(launcherView,
				LinearLayout.LayoutParams.FILL_PARENT,
				formatter.getFlipperViewHeight(tabformatter));
		
				
		this.addView(tabRowView,
				LinearLayout.LayoutParams.FILL_PARENT,
				tabformatter.getTabHeight());
		
		this.addView(topTabView,
				LinearLayout.LayoutParams.FILL_PARENT,
				formatter.getTopTabHeight());
		

	}

	private void setViewLayout(Context context, CellInfo[][] data,
			Formatter formatter, int numColsPerPage) {
		this.setOrientation(LinearLayout.VERTICAL);
		
	}

	public LauncherView getLauncherView() {
		return launcherView;
	}

	public TabRowView getTabRowView() {
		return tabRowView;
	}
}
